package src.TwoPhaseTermination;

/**
 * Created by 58 on 2016/3/29.
 */
public class CountupThread extends Thread {
    private long counter = 0;
    private volatile boolean shutdownRequested = false;

    public void shutdownRequest() {
        shutdownRequested = true;
        interrupt();
    }

    @Override
    public void run() {
        try {
            while (!shutdownRequested) {
                doWork();
            }
        } catch (InterruptedException e) {
            System.out.println("interruptedException invoked");
        } finally {
            doShutDown();
        }
    }

    private void doWork() throws InterruptedException {
        counter++;
        System.out.println("doWork: counter = " + counter);
        Thread.sleep(2000);
    }

    // 终止条件
    private void doShutDown() {
        System.out.println("doWork: counter = " + counter);
    }
}
